<?php
 echo ("Updating User Table");
   $con = mysqli_connect("localhost", "root", "", "soccer_2013");
      if ($con->connect_errno) {
         echo "Failed to connect to MySQL: " . $con->connect_errno;
      } else {
          //PSEUDOCODE
          //1 Get the variables from form
          $team1 = $_GET['team1'];
          $team2 = $_GET['team2'];
          $team1score = $_GET['team1score'];
          $team2score = $_GET['team2score'];
          echo (" team1 id is $team1 <br />");
           echo (" team2 id is $team2<br />");
           echo (" team1 scored  $team1score<br />");
           echo (" team2 scored $team2score<br />");
          //2 Validate variables and load existing values
           $existQ1 = "SELECT played, won, drawn FROM psl WHERE `team_id`=$team1";
           $resultQ1 = $con->query($existQ1);
           $rowQ1 = $resultQ1->fetch_array();
           $existQ2 = "SELECT played, won, drawn FROM psl WHERE `team_id`=$team2";
           $resultQ2 = $con->query($existQ2);
           $rowQ2 = $resultQ2->fetch_array();
           
           $t1played = $rowQ1[0] ;//stores played for team 1
           $t1won = $rowQ1[1] ;//stores won for team 1
           $t1draw = $rowQ1[2] ;//stores drawn for team 1
           $t2played = $rowQ2[0] ;//stores played for team 2
           $t2won = $rowQ2[1] ;//stores won for team 2
           $t2draw = $rowQ2[2] ;//stores drawn for team 2
           
          //3 Do the required calculations
          
           if ($team1score > $team2score) {
              $t1won++;
              $t1played++;
              $t2played++;
           }
           if ($team2score > $team1score) {             
              $t2won++;
              $t1played++;
              $t2played++;
           }
           if ($team2score == $team1score) {             
              $t1draw++;
              $t2draw++;
              $t1played++;
              $t2played++;
           }
         // 4 Construct the update statement 
            $t1update = "UPDATE psl SET played='$t1played', won='$t1won'
                , drawn='$t1draw' where `team_id`='$team1'";
            $t2update = "UPDATE psl SET played='$t2played', won='$t2won'
                , drawn='$t2draw' where `team_id`='$team2'";
          //5 Apply the statement
            $r1 = $con->query($t1update);
            $r2 = $con->query($t2update);
          

      }
    $con->close();
?>
